/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package entity;

import com.github.dvdme.ForecastIOLib.FIODataPoint;
import java.io.Serializable;
import static java.lang.System.out;
import java.util.Collection;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import resource.WeatherWrapper;

/**
 *
 * @author Jov
 */
@Entity
@Table(name = "events")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Events.findAll", query = "SELECT e FROM Events e"),
    @NamedQuery(name = "Events.findByEventId", query = "SELECT e FROM Events e WHERE e.eventId = :eventId"),
    @NamedQuery(name = "Events.findByEventName", query = "SELECT e FROM Events e WHERE e.eventName = :eventName"),
    @NamedQuery(name = "Events.findByEventYear", query = "SELECT e FROM Events e WHERE e.eventYear = :eventYear"),
    @NamedQuery(name = "Events.findByEventMonth", query = "SELECT e FROM Events e WHERE e.eventMonth = :eventMonth"),
    @NamedQuery(name = "Events.findByEventDay", query = "SELECT e FROM Events e WHERE e.eventDay = :eventDay"),
    @NamedQuery(name = "Events.findByEventHour", query = "SELECT e FROM Events e WHERE e.eventHour = :eventHour"),
    @NamedQuery(name = "Events.findByEventMinute", query = "SELECT e FROM Events e WHERE e.eventMinute = :eventMinute"),
    @NamedQuery(name = "Events.findByDuration", query = "SELECT e FROM Events e WHERE e.duration = :duration"),
    @NamedQuery(name = "Events.findByLocation", query = "SELECT e FROM Events e WHERE e.location = :location"),
    @NamedQuery(name = "Events.findByPrivate1", query = "SELECT e FROM Events e WHERE e.private1 = :private1"),
    @NamedQuery(name = "Events.findByOutdoor", query = "SELECT e FROM Events e WHERE e.outdoor = :outdoor"),
    @NamedQuery(name = "Events.findByCreator", query = "SELECT e FROM Events e WHERE e.creator = :creator"),
    @NamedQuery(name = "Events.findByDesiredWeather", query = "SELECT e FROM Events e WHERE e.desiredWeather = :desiredWeather"),
    @NamedQuery(name = "Events.findByForcWeatherType", query = "SELECT e FROM Events e WHERE e.forcWeatherType = :forcWeatherType"),
    @NamedQuery(name = "Events.findByForcTemp", query = "SELECT e FROM Events e WHERE e.forcTemp = :forcTemp"),
    @NamedQuery(name = "Events.findByForcRainProb", query = "SELECT e FROM Events e WHERE e.forcRainProb = :forcRainProb"),
    @NamedQuery(name = "Events.findByForcWind", query = "SELECT e FROM Events e WHERE e.forcWind = :forcWind"),
    @NamedQuery(name = "Events.findByForcHumidity", query = "SELECT e FROM Events e WHERE e.forcHumidity = :forcHumidity"),
    @NamedQuery(name = "Events.updateForcWeather", query= "UPDATE Events e SET e.forcWeatherType = :forcWeatherType, e.forcRainProb = :forcRainProb, e.forcTemp = :forcTemp, e.forcWind = :forcWind, e.forcHumidity = :forcHumidity WHERE e.eventId = :eventId")})
public class Events implements Serializable {
    @Column(name = "outdoor")
    private Boolean outdoor;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "events")
    private Collection<Participants> participantsCollection;
    @JoinTable(name = "participants", joinColumns = {
        @JoinColumn(name = "event_id", referencedColumnName = "event_id")}, inverseJoinColumns = {
        @JoinColumn(name = "participant_id", referencedColumnName = "user_id")})
    @ManyToMany
    private Collection<Users> usersCollection;
    @OneToMany(mappedBy = "event")
    private Collection<Invitations> invitationsCollection;
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "event_id")
    private Integer eventId;
    @Size(max = 100)
    @Column(name = "event_name")
    private String eventName;
    @Column(name = "event_year")
    private Integer eventYear;
    @Column(name = "event_month")
    private Integer eventMonth;
    @Column(name = "event_day")
    private Integer eventDay;
    @Column(name = "event_hour")
    private Integer eventHour;
    @Column(name = "event_minute")
    private Integer eventMinute;
    @Column(name = "duration")
    private Integer duration;
    @Size(max = 50)
    @Column(name = "location")
    private String location;
    @Column(name = "private")
    private Boolean private1;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 50)
    @Column(name = "desiredWeather")
    private String desiredWeather;
    @Size(max = 50)
    @Column(name = "forcWeatherType")
    private String forcWeatherType;
    @Column(name = "forcTemp")
    private Double forcTemp;
    @Column(name = "forcRainProb")
    private Double forcRainProb;
    @Column(name = "forcWind")
    private Double forcWind;
    @Column(name = "forcHumidity")
    private Double forcHumidity;
    @JoinColumn(name = "creator", referencedColumnName = "user_id")
    @ManyToOne(optional = false)
    private Users creator;

    public Events() {
    }

    public Events(Integer eventId) {
        this.eventId = eventId;
    }

    public Events(Integer eventId, String desiredWeather) {
        this.eventId = eventId;
        this.desiredWeather = desiredWeather;
    }

    public void updateWeatherInfo(){
        WeatherWrapper ww = new WeatherWrapper();
        ww.setCity(location);
        if(ww.getSpecificDay(this.eventDay)==null) {
            out.println("non c'è il tempo per il giorno"+eventDay);
            return;}
        this.forcWeatherType = ww.getSpecificDay(this.eventDay).icon();
        this.forcTemp = (ww.getSpecificDay(this.eventDay).temperatureMax()+ww.getSpecificDay(this.eventDay).temperatureMin())/2;
        this.forcRainProb = ww.getSpecificDay(this.eventDay).precipProbability()*100;
        this.forcWind = ww.getSpecificDay(this.eventDay).windSpeed();
        this.forcHumidity = ww.getSpecificDay(this.eventDay).humidity()*100;
    }
    
    public Integer getEventId() {
        return eventId;
    }

    public void setEventId(Integer eventId) {
        this.eventId = eventId;
    }

    public String getEventName() {
        return eventName;
    }

    public void setEventName(String eventName) {
        this.eventName = eventName;
    }

    public Integer getEventYear() {
        return eventYear;
    }

    public void setEventYear(Integer eventYear) {
        this.eventYear = eventYear;
    }

    public Integer getEventMonth() {
        return eventMonth;
    }

    public void setEventMonth(Integer eventMonth) {
        this.eventMonth = eventMonth;
    }

    public Integer getEventDay() {
        return eventDay;
    }

    public void setEventDay(Integer eventDay) {
        this.eventDay = eventDay;
    }

    public Integer getEventHour() {
        return eventHour;
    }

    public void setEventHour(Integer eventHour) {
        this.eventHour = eventHour;
    }

    public Integer getEventMinute() {
        return eventMinute;
    }

    public void setEventMinute(Integer eventMinute) {
        this.eventMinute = eventMinute;
    }

    public Integer getDuration() {
        return duration;
    }

    public void setDuration(Integer duration) {
        this.duration = duration;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public Boolean getPrivate1() {
        return private1;
    }

    public void setPrivate1(Boolean private1) {
        this.private1 = private1;
    }

    public Boolean getOutdoor() {
        return outdoor;
    }

    public void setOutdoor(Boolean outdoor) {
        this.outdoor = outdoor;
    }

    public String getDesiredWeather() {
        return desiredWeather;
    }

    public void setDesiredWeather(String desiredWeather) {
        this.desiredWeather = desiredWeather;
    }

    public String getForcWeatherType() {
        return forcWeatherType;
    }

    public void setForcWeatherType(String forcWeatherType) {
        this.forcWeatherType = forcWeatherType;
    }

    public Double getForcTemp() {
        return forcTemp;
    }

    public void setForcTemp(Double forcTemp) {
        this.forcTemp = forcTemp;
    }

    public Double getForcRainProb() {
        return forcRainProb;
    }

    public void setForcRainProb(Double forcRainProb) {
        this.forcRainProb = forcRainProb;
    }

    public Double getForcWind() {
        return forcWind;
    }

    public void setForcWind(Double forcWind) {
        this.forcWind = forcWind;
    }

    public Double getForcHumidity() {
        return forcHumidity;
    }

    public void setForcHumidity(Double forcHumidity) {
        this.forcHumidity = forcHumidity;
    }

    public Users getCreator() {
        return creator;
    }

    public void setCreator(Users creator) {
        this.creator = creator;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (eventId != null ? eventId.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Events)) {
            return false;
        }
        Events other = (Events) object;
        if ((this.eventId == null && other.eventId != null) || (this.eventId != null && !this.eventId.equals(other.eventId))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "entity.Events[ eventId=" + eventId + " ]";
    }

    @XmlTransient
    public Collection<Invitations> getInvitationsCollection() {
        return invitationsCollection;
    }

    public void setInvitationsCollection(Collection<Invitations> invitationsCollection) {
        this.invitationsCollection = invitationsCollection;
    }

    @XmlTransient
    public Collection<Users> getUsersCollection() {
        return usersCollection;
    }

    public void setUsersCollection(Collection<Users> usersCollection) {
        this.usersCollection = usersCollection;
    }

    @XmlTransient
    public Collection<Participants> getParticipantsCollection() {
        return participantsCollection;
    }

    public void setParticipantsCollection(Collection<Participants> participantsCollection) {
        this.participantsCollection = participantsCollection;
    }

    
}
